package com.sanpu.kxe.business.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.library.imagechoose.PhotoPicker;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.OldAuthThirdStepContract;
import com.sanpu.kxe.business.main.presenter.OldAuthThirdStepPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.business.main.view.CertificateSecondActivity;
import com.sanpu.kxe.business.main.view.OldCertificateSecondActivity;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.eventbus.ReviewPhotoEvent;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.rxservice.RequestMap;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;

import static com.sanpu.kxe.R.id.crdConCloud;
import static com.sanpu.kxe.R.id.crdConIv;
import static com.sanpu.kxe.R.id.crdFroCloud;
import static com.sanpu.kxe.R.id.crdFroIv;
import static com.sanpu.kxe.R.id.handCloud;
import static com.sanpu.kxe.R.id.handIv;

/**
 * Description：认证人工审核第三步
 * Created by 薛勇军 on 2019/5/15.
 */

public class OldAuthThirdStepFragment extends BaseFragment implements OldAuthThirdStepContract.View {

    @Bind(crdFroIv)
    ImageView vCrdFroIv;
    @Bind(R.id.crdFro)
    ImageView vCrdFro;
    @Bind(crdFroCloud)
    TextView vCrdFroCloud;

    @Bind(crdConIv)
    ImageView vCrdConIv;
    @Bind(R.id.crdCon)
    ImageView vCrdCon;
    @Bind(crdConCloud)
    TextView vCrdConCloud;

    @Bind(handIv)
    ImageView vHandIv;
    @Bind(R.id.hand)
    ImageView vHand;
    @Bind(handCloud)
    TextView vHandCloud;

    private OldAuthThirdStepContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_auth_third_old;
    }

    @Override
    protected void initView() {
        super.initView();
        HashMap<String, File> mUploadFiles = ((OldCertificateSecondActivity) getActivity()).getmUploadFiles();
        if (mUploadFiles == null)
            mUploadFiles = new HashMap<>();
        CertificationOldOutput mOutput = (CertificationOldOutput)
                getArguments().getSerializable(MainConfig.INTENT_CERTIFICATION_OLD_INPUT);
        mPresenter = new OldAuthThirdStepPresenter(this, mUploadFiles, mOutput);
        if (mOutput != null && !TextUtils.equals(CommonConfig.AUTHEN_NOTSUB, mOutput.getChkSts())) {
            ((BaseTitleActivity) getActivity()).showLoadingDialog(false, "正在加载图片，请稍后...");
            parseAndDisplayImage(mUploadFiles.get(CommonConfig.TYPE_CRDFROPIC),
                    mOutput.getCrdFroPic(), vCrdFroIv, vCrdFroCloud, CommonConfig.TYPE_CRDFROPIC);

            parseAndDisplayImage(mUploadFiles.get(CommonConfig.TYPE_CRDCONPIC),
                    mOutput.getCrdConPic(), vCrdConIv, vCrdConCloud, CommonConfig.TYPE_CRDCONPIC);

            parseAndDisplayImage(mUploadFiles.get(CommonConfig.TYPE_IDHANDPIC),
                    mOutput.getIdHandPic(), vHandIv, vHandCloud, CommonConfig.TYPE_IDHANDPIC);
        }
    }

    @OnClick({R.id.commit, R.id.lastStepRtv, R.id.crdFro, R.id.crdCon, R.id.hand})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit:
                mPresenter.onCommitData();
                break;
            case R.id.lastStepRtv:
                ((CertificateSecondActivity) getActivity()).switchPageSecond();
                break;
            case R.id.crdFro:
                if (vCrdFroIv.getTag() != null) {
                    mPresenter.takePhoto((String) vCrdFroIv.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDFRO);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_CRDFROPIC);
                }
                break;
            case R.id.crdCon:
                if (vCrdConIv.getTag() != null) {
                    mPresenter.takePhoto((String) vCrdConIv.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDCON);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_CRDCONPIC);
                }
                break;
            case R.id.hand:
                if (vHandIv.getTag() != null) {
                    mPresenter.takePhoto((String) vHandIv.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_HAND);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_IDHANDPIC);
                }
                break;
        }
    }

    /**
     * 照片回显
     */
    public void parseAndDisplayImage(File picStr, String imageUrl, ImageView imageView,
                                     TextView textView, String fileName) {
        if (picStr != null && picStr.exists()) {
            imageView.setTag(picStr.getPath());
            JKImageLoader.getInstance().displayImage(getContext(), picStr, imageView);
            textView.setVisibility(View.VISIBLE);
        } else if (!TextUtils.isEmpty(imageUrl)) {
            String imgUrl = UrlConstants.getBaseUrl() + imageUrl + RequestMap.sessionIdReqStr();
            imageView.setTag(imgUrl);
            JKImageLoader.getInstance().displayImage(getContext(), imgUrl, imageView);
            textView.setVisibility(View.VISIBLE);
            mPresenter.getImageFileByNet(imgUrl, fileName);
        }
    }

    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null) {
            ArrayList<String> photos =
                    data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            if (photos == null || photos.size() == 0) {
                switch (requestCode) {
                    case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDFRO:
                        vCrdFroIv.setTag(null);
                        vCrdFroIv.setImageDrawable(null);
                        vCrdFroIv.setVisibility(View.GONE);
                        vCrdFroCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_CRDFROPIC);
                        break;
                    case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDCON:
                        vCrdConIv.setTag(null);
                        vCrdConIv.setImageDrawable(null);
                        vCrdConIv.setVisibility(View.GONE);
                        vCrdConCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_CRDCONPIC);
                        break;
                    case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_HAND:
                        vHandIv.setTag(null);
                        vHandIv.setImageDrawable(null);
                        vHandIv.setVisibility(View.GONE);
                        vHandCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_IDHANDPIC);
                        break;
                }
            }
        }
    }

    @Override
    public void refreshView(ReviewPhotoEvent event) {
        String filePath = event.getFilePath();
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        String fileIdName;
        switch (event.getCurrentType()) {
            case CommonConfig.TYPE_CRDFROPIC:
                fileIdName = CommonConfig.TYPE_CRDFROPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vCrdFroIv);
                vCrdFroIv.setTag(filePath);
                vCrdFroIv.setVisibility(View.VISIBLE);
                vCrdFroCloud.setVisibility(View.VISIBLE);
                break;
            case CommonConfig.TYPE_CRDCONPIC:
                fileIdName = CommonConfig.TYPE_CRDCONPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vCrdConIv);
                vCrdConIv.setTag(filePath);
                vCrdConIv.setVisibility(View.VISIBLE);
                vCrdConCloud.setVisibility(View.VISIBLE);
                break;
            case CommonConfig.TYPE_IDHANDPIC:
                fileIdName = CommonConfig.TYPE_IDHANDPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vHandIv);
                vHandIv.setTag(filePath);
                vHandIv.setVisibility(View.VISIBLE);
                vHandCloud.setVisibility(View.VISIBLE);
                break;
            default:
                return;
        }
        mPresenter.onCompress(filePath, fileIdName);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(OldAuthThirdStepContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void showLoadingDialog(boolean cancelable, String content) {
        ((BaseTitleActivity)getActivity()).showLoadingDialog(cancelable, content);
    }

    @Override
    public void dismissLoadingDialog() {
        ((BaseTitleActivity)getActivity()).dismissLoadingDialog();
    }

    @Override
    public void showErrorText(Throwable e) {
        ((BaseTitleActivity)getActivity()).showErrorText(e);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
